package cn.yyh.commons.utils;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**返回给前台的结果集
 * @author 杨永航
 * @version 1.0
 * @Title: R
 * @Package: cn.yyh.basic.utils
 * @date 2021/6/21
 */
@Data
public class R {
    /** 提示信息 */
    private String message;
    /** 状态码 */
    private Integer code;
    /** 时候成功 */
    private Boolean success;
    /** 返回数据集合 */
    private Map<String,Object> data = new HashMap<>();

    private Object resultObj;

    private R() {
    }

    /**
     * 成功时调用
     * @version: 1.0
     * @return: R
     * @author: 杨永航
     * @date: 2021/6/23
     */
    public static R oK(){
        R r = new R();
        r.setCode(20000);
        r.setSuccess(true);
        r.setMessage("成功");
        return r;
    }

    /**
     * 失败时调用
     * @version: 1.0
     * @return: R
     * @author: 杨永航
     * @date: 2021/6/23
     */
    public static R error(){
        R r = new R();
        r.setCode(20001);
        r.setSuccess(false);
        r.setMessage("失败");
        return r;
    }

    public R message(String msg){
        this.setMessage(msg);
        return this;
    }

    public R code(Integer code){
        this.setCode(code);
        return this;
    }

    public R success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    /**
     * 链式编程.传入key,value放入map集合
     * @version: 1.0
     * @param str key
     * @param obj value
     * @return: R
     * @author: 杨永航
     * @date: 2021/6/23
     */
    public R data(String str,Object obj){
        this.data.put(str, obj);
        return this;
    }

    /**
     * 方法重载.传入一个集合
     * @version: 1.0
     * @param map 数据集合
     * @return: R
     * @author: 杨永航
     * @date: 2021/6/23
     */
    public R data(Map<String,Object> map){
        this.data = map;
        return this;
    }

    public R setResultObj(Object resultObj) {
        this.resultObj = resultObj;
        return this;
    }
}
